/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.info.service.plugins  
 */
package com.apache.info.service.plugins;

import java.util.List;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.info.entity.PubMetadata;
import com.apache.info.entity.PubSortInfo;
import com.apache.info.manager.InfoReleaseManager;
import com.apache.info.manager.PubMetadataManager;
import com.apache.util.ConfigUtil;

/**
 * description:  动态创建表操作
 * @author Hou Dayu 创建时间：2015-8-28  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class DynamicCreateTablePluinImpl implements PluginConnector {

	private InfoReleaseManager infoReleaseManager;
	private PubMetadataManager pubMetadataManager;

	/**
	 * TODO 动态创建表插件实现.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo vo) throws Exception {
		PubSortInfo sort = (PubSortInfo) vo.getParams("sort");
		ParamsVo<PubMetadata> met = new ParamsVo<PubMetadata>();
		PubMetadata pm = new PubMetadata();
		pm.setSortId(sort.getSortId());
		met.setObj(pm);
		List<PubMetadata> list = pubMetadataManager.getList(met);
		if (!Validator.isEmpty(list)) {
			String dbType = ConfigUtil.getInstance().getValueByKey("jdbc.properties", "database_type");
			String sql = "";
			if ("oracle".equals(dbType)) {
				sql = createTableSqlForOracle(list, sort.getInfoTableName());
			} else if ("sqlserver".equals(dbType)) {
				sql = createTableSqlForSqlserver(list, sort.getInfoTableName());
			} else if ("myssql".equals(dbType)) {
				sql = createTableSqlForMySql(list, sort.getInfoTableName());
			}
			ParamsVo pvo = new ParamsVo();
			pvo.setParams("dyncSql", sql);
			pvo.setParams("sort", sort);
			return infoReleaseManager.editInfo(pvo);
		}
		return true;
	}

	/**
	 * description: oracle sql 
	 */
	private String createTableSqlForOracle(List<PubMetadata> list, String tableName) {
		StringBuffer sql = new StringBuffer("create table " + tableName + "(");
		String pk = "";
		for (PubMetadata pm : list) {
			sql.append(" " + pm.getDataShortName() + " ");
			if ("1".equals(pm.getDataType()) || "2".equals(pm.getDataType())) {
				sql.append(" number(" + pm.getDataLength() + ") ");
			} else if ("3".equals(pm.getDataType())) {
				sql.append(" number(" + pm.getDataLength() + ",2) ");
			} else if ("4".equals(pm.getDataType())) {
				sql.append(" char(" + pm.getDataLength() + ") ");
			} else if ("6".equals(pm.getDataType())) {
				sql.append(" date ");
			} else {
				sql.append(" varchar2(" + pm.getDataLength() + ") ");
			}

			if ("1".equals(pm.getDataRestrict())) {
				sql.append(" not null ");
				pk = pm.getDataShortName();
			} else if ("2".equals(pm.getDataRestrict())) {
				sql.append(" not null ");
			}
			sql.append(",");
		}
		if (Validator.isNotNull(pk))
			sql.append("primary key(" + pk + ")");
		sql.append(")");
		return sql.toString();
	}

	/**
	 * description: sqlserver sql 
	 */
	private String createTableSqlForSqlserver(List<PubMetadata> list, String tableName) {
		StringBuffer sql = new StringBuffer("create table " + tableName + "(");
		int count = list.size();
		int i = 0;
		for (PubMetadata pm : list) {
			sql.append(" " + pm.getDataShortName() + " ");
			if ("1".equals(pm.getDataType())) {
				sql.append(" int ");
			} else if ("2".equals(pm.getDataType())) {
				sql.append(" bigint ");
			} else if ("3".equals(pm.getDataType())) {
				sql.append(" float");
			} else if ("4".equals(pm.getDataType())) {
				sql.append(" char(" + pm.getDataLength() + ") ");
			} else if ("6".equals(pm.getDataType())) {
				sql.append(" datetime ");
			} else {
				sql.append(" varchar(" + pm.getDataLength() + ") ");
			}

			if ("1".equals(pm.getDataRestrict())) {
				sql.append(" primary key ");
			} else if ("2".equals(pm.getDataRestrict())) {
				sql.append(" not null ");
			}
			if (i < count) {
				sql.append(",");
			}
			i++;
		}
		sql.append(")");
		return sql.toString();
	}

	/**
	 * description: mysql sql 
	 */
	private String createTableSqlForMySql(List<PubMetadata> list, String tableName) {
		StringBuffer sql = new StringBuffer("create table " + tableName + "(");
		String pk = "";
		for (PubMetadata pm : list) {
			sql.append(" " + pm.getDataShortName() + " ");
			if ("1".equals(pm.getDataType())) {
				sql.append(" int ");
			} else if ("2".equals(pm.getDataType())) {
				sql.append(" bigint ");
			} else if ("3".equals(pm.getDataType())) {
				sql.append(" float");
			} else if ("4".equals(pm.getDataType())) {
				sql.append(" char(" + pm.getDataLength() + ") ");
			} else if ("6".equals(pm.getDataType())) {
				sql.append(" datetime ");
			} else {
				sql.append(" varchar(" + pm.getDataLength() + ") ");
			}

			if ("1".equals(pm.getDataRestrict())) {
				sql.append(" not null ");
				pk = pm.getDataShortName();
			} else if ("2".equals(pm.getDataRestrict())) {
				sql.append(" not null ");
			}
			sql.append(",");
		}
		if (Validator.isNotNull(pk))
			sql.append("primary key(" + pk + ")");
		sql.append(")");
		return sql.toString();
	}

	public void setInfoReleaseManager(InfoReleaseManager infoReleaseManager) {
		this.infoReleaseManager = infoReleaseManager;
	}

	public void setPubMetadataManager(PubMetadataManager pubMetadataManager) {
		this.pubMetadataManager = pubMetadataManager;
	}

}
